package DAO;

import Base.Dados;
import Base.Resposta;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Rosana
 */
public class DaoResposta {

    public boolean inserirResposta(Resposta resp)
    {
        Connection con = FactoryConnection.getConnection();
        ResultSet rs;
        int cod = -1;
        
        if(resp.getId() ==-1)
        {
            String query = "INSERT INTO resposta(pergunta, resposta, correta)"
                    + " VALUES(?,?,?)";

            try
            {
                CallableStatement stmt = con.prepareCall(query);
                stmt.setInt(1, resp.getIdPergunta());
                stmt.setString(2, resp.getDescricao());
                stmt.setBoolean(3, resp.isCorreta());
                stmt.execute();
                
                stmt.close();                  
            }
            catch (SQLException e)
            {
                System.out.println("Erro no SQL do inserir resposta");
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
    
    public boolean excluir(Resposta resp)
    {
        Connection con = FactoryConnection.getConnection();
        ResultSet rs;
        String query = "DELETE FROM resposta WHERE id =?";

        try
        {
            CallableStatement stmt = con.prepareCall(query);
            stmt.setInt(1, resp.getId());                
            stmt.execute();                
            stmt.close();  

        }
        catch (SQLException e)
        {
            System.out.println("Erro no SQL do excluir resposta");
            e.printStackTrace();
            return false;
        }

        return true;
    }

    boolean excluirVarios(Dados dados) {
        Connection con = FactoryConnection.getConnection();
        String query = "DELETE FROM resposta WHERE id in (?,?,?,?)";
        System.out.println("entrou no excluir Varios");
        try
        {
            CallableStatement stmt = con.prepareCall(query);
            stmt.setInt(1, dados.getResp1().getId());                
            stmt.setInt(2, dados.getResp2().getId());                
            stmt.setInt(3, dados.getResp3().getId());                
            stmt.setInt(4, dados.getResp4().getId());                
            stmt.execute();                
            stmt.close();  

        }
        catch (SQLException e)
        {
            System.out.println("Erro no SQL do excluir várias respostas");
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    public void updateResposta(String res, boolean cor, int id) {
        Connection con = FactoryConnection.getConnection();
        String query = "UPDATE resposta SET resposta = ?, correta = ? where id = ?";
        try
        {
            CallableStatement stmt = con.prepareCall(query);
            stmt.setString(1, res);                
            stmt.setBoolean(2, cor);                
            stmt.setInt(3, id);              
            stmt.executeUpdate();                
            stmt.close();  

        }
        catch (SQLException e)
        {
            System.out.println("Erro no SQL do update pergunta");
            e.printStackTrace();            
        }
        
    }

}
